package com.abuaziz.lib_processor

//import com.google.auto.service.AutoService
import com.abuaziz.annotation.Clone
import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.*
import java.io.File
import java.util.*
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.PackageElement
import javax.lang.model.element.TypeElement


@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
class MyProcessor : AbstractProcessor() {

    lateinit var filer: Filer

    override fun init(p0: ProcessingEnvironment) {
        super.init(p0)
        filer = p0.filer
    }

    private fun FileSpec.writeFile() {
        //文件编译后位置
        val kaptKotlinGeneratedDir = processingEnv.options["kapt.kotlin.generated"]
        val outputFile = File(kaptKotlinGeneratedDir).apply {
            mkdirs()
        }
        writeTo(outputFile.toPath())
    }

    override fun process(typeElement: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
//        val `package` = "com.abuaziz.librarys"
        val `package` = ""


//        roundEnv.getElementsAnnotatedWith(Clone::class.java).forEach {
//            var element = it as TypeElement

//            var type =  element.asType()

//            Class.forName("")


//        }

//        roundEnv.getElementsAnnotatedWith(Clone::class.java).forEach {
//            var elementType = it as TypeElement
//        }



        val greeterClass = ClassName(`package`, "Greeter")
        val file = FileSpec.builder(`package`, "HelloWorld")
                .addType(TypeSpec.classBuilder("Greeter")
                        .primaryConstructor(FunSpec.constructorBuilder()
                                .addParameter("name", String::class)
                                .build())
                        .addProperty(PropertySpec.builder("name", String::class)
                                .initializer("name")
                                .build())
                        .addFunction(FunSpec.builder("greet")
                                .addStatement("println(%P)", "Hello, \$name")
                                .addStatement("println(%P)", "roundEnv.szie = ${roundEnv.getElementsAnnotatedWith(Clone::class.java).size}")
                                .addStatement("println(%P)", "typeElement.szie = ${typeElement.size}")
                                .build())
                        .build())
                .addFunction(FunSpec.builder("main")
                        .addParameter("args", String::class, KModifier.VARARG)
                        .addStatement("%T(args[0]).greet()", greeterClass)
                        .build())
                .build()





        file.writeFile()

//        var classBuilder = TypeSpec.classBuilder("HelloWorld")
//                .addModifiers(KModifier.PUBLIC)
//                .build()
//        file.writeTo(filer)
//        val dir = File("code")
//        dir.mkdirs()
//        file.writeTo(dir)
        return true
    }

    override fun getSupportedSourceVersion(): SourceVersion {
        return SourceVersion.latest()
    }

    override fun getSupportedAnnotationTypes(): Set<String> {
//        super.getSupportedAnnotationTypes()
        return setOf(Clone::class.java.canonicalName)

    }
}